home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18058 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  115 lines

  1. Path: in1.uu.net!demos!usenet
  2. From: Alexey Ruzin <00alex@dbs.demos.su>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: BC 3.1 - 4.5 (error!?) ATTENTION
  5. Date: Thu, 18 Apr 1996 22:32:05 +0400
  6. Organization: Demos Online Service
  7. Message-ID: <31768AA5.420E@dbs.demos.su>
  8. References: <3173D628.48F6@demos.su> <4l5gn1$gtb@Grouper.Exis.Net>
  9. NNTP-Posting-Host: 00alex@dbs.demos.su
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
  14.  
  15. Tim Lawrence wrote:
  16. > Tested in BC 4.5 and 5.0.  Results where what I expected.  The reason you
  17. > may think you are getting unpredictable results is the use of a static var
  18. > in a function.
  19. > In article <3173D628.48F6@demos.su>, 00alex@demos.su says...
  20. > >
  21. > >Hi, All
  22. > >
  23. > >Here is simple example of abnormal programming.
  24. > >But BC crashed (sorry, it works and even compiles
  25. > >the program, but it is unpredictable what will occur)
  26. > >on it:
  27.  
  28. Klaus Eichele wrote:
  29. > Hi,
  30. > I am not sure what this program is supposed to achieve, but compiling
  31. > it sure produces an error: illegal initialization.
  32. > >  static int p=a;
  33. > The reason is that at initialization time a is (or would be)
  34. > undefined. If you want your function to work, you will need to change
  35. > it to:
  36.  
  37.  
  38. Thanks to all, who reply on my message. I think i need to
  39. explain some things in hronological sequence.
  40. Once my friend wrote a program. He need to store incoming value
  41. in some function at first call and do something else with
  42. this value at next calls. He wants initialize 'static' variable
  43. with incoming value:
  44.  
  45. int func( int i )
  46. {
  47.     static int sv = a;
  48. ...
  49. }
  50.  
  51. But i thought there are two ways:
  52. 1. it is impossible (complier initialize variable at start of
  53.    whole program [i believe memory is allocated at that stage] and 
  54.    it doesn't know with what value to do it) -> ERROR ON COMPLILE.
  55. 2. compiler spend some resources to store is that was first or
  56.    sequenced call and initializing is going normally -> COMPILE OK.
  57.  
  58. We decided to check out what does complier. So we wrote simple
  59. program:
  60.  
  61. #include <stdio.h>
  62. int func( int a )
  63. {
  64.     static int sv = a;
  65.     return sv++;
  66. }
  67. void main()
  68. {
  69.     printf("%i\n", func(1) );
  70. }
  71.  
  72. That compiles perfectly so we decided that compiler goes 2 way :).
  73. But we were still not sure that complier does all fine and make
  74. assembler source (compile via asm, generate asm source).
  75. And what did we see??? It initialize static int sv with 1!!!!!
  76. We were doubted. Then we begin modify our source to beat the
  77. compiler. Next program was:
  78.  
  79. #include <stdio.h>
  80. int func( int a )
  81. {
  82.     static int sv = a;
  83.     return sv++;
  84. }
  85. void main( int argc, char *argv[] )
  86. {
  87.     printf("%i\n", func(argc) );
  88. }
  89.  
  90. And what do you think? Compiler initialyze (from asm source) 
  91. static int sv with value is laying in address of argc. BTW all
  92. these initializations was done before main starts :).
  93.  
  94. Last program which was represented in my first message beat compiler.
  95. It begins to print numbers begining with strange 825!!!
  96. (that was really so on my computer)
  97. I test last variant of program with SunOs compiler, 
  98. it say an ERROR (1 way): non-constant initializer. I'v got that here
  99. is all right.
  100.  
  101. All what was written here and in my first message is not stupid yell.
  102. But I was wondered why did you get normal results and I didn't.
  103.  
  104. While i wrote this answer I tried to test all fragments with
  105. SunOs compiler. And, it's tariffic, I'v got what was a problem:
  106. C and C++ sources are generated different ways (it's my opinion).
  107.   C:   1 way;
  108.   C++: 2 way.
  109. Just I renamed my test file I got what I wait.
  110. Unfortunately I forgot what C or C++ source I generated with BC
  111. but I am sure bug WAS!!! 
  112.  
  113. Thanks to all for attention, that was my fault.
  114.